package zhr.chapter8;

import java.util.ArrayList;
import java.util.Iterator;

public class Example8_6 {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Student stu1 = new Student();
        stu1.setStuName("李雷");
        stu1.setStuAge(21);
        students.add(0, stu1);
        Student stu2 = new Student();
        stu2.setStuName("韩梅梅");
        stu2.setStuAge(22);
        students.add(stu2);
        Student stu3 = new Student();
        stu3.setStuName(" Jack");
        stu3.setStuAge(20);
        students.add(stu3);
        System.out.println("当前学生人数为: " + students.size() + ",分别为: ");
        Iterator iterator = students.iterator();
        while (iterator.hasNext()) {
            Student stu = (Student) iterator.next();
            System.out.println(stu.printStuInfo());
        }
        if (students.contains(stu1)) {
            System.out.println("李雷存在于当前列表 students中.");
        }
        ArrayList studentsCopy = (ArrayList) students.clone();
        Student stu1Copy = (Student) studentsCopy.get(0);
        stu1Copy.setStuAge(30);
        System.out.println(" students的克隆列表,对第一个元素修改后:");
        iterator = students.iterator();
        while (iterator.hasNext()) {
            Student stu = (Student) iterator.next();
            System.out.println(stu.printStuInfo());
        }
        if (students.remove(stu3)) {
            System.out.println(stu3.printStuInfo() + "被从students中移除.");
        }
        System.out.println("经过一系列处理后的students列表:");
        Object[] s = students.toArray();
        for (Object o : s) {
            System.out.println(((Student) o).printStuInfo());
        }
    }
}